package com.zyx.service;

/**
  * @author zyx
  * @date 2022/1/12
 * HttpServletRequest request, HttpServletResponse response, HttpSession session
  */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zyx.mapper.OrdersMapper;
import com.zyx.pojo.*;
import com.zyx.vo.GetSession;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.*;

@Service
public class OrderService {
    @Resource
    private OrdersMapper mapper;


    /**
     * 查询所有预约看车数和每日新增数量
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public Map<String,Integer> queryAll(){
        OrdersExample example = new OrdersExample();
        int l = mapper.countByExample(example);
        int i = mapper.queryByTime();
        Map<String,Integer> map = new HashMap<>();
        map.put("data_size",l);
        map.put("data_new",i);
        return map;
    }

    /**
     * 根据查询某个店铺预约看车数和每日新增数量
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)
    public Map<String,Integer> queryAllSpencer(HttpServletRequest request, HttpServletResponse response, HttpSession session){
        GetSession getSession = new GetSession();
        Spencer spencer = getSession.spencerSession(request, response, session);
        OrdersExample example = new OrdersExample();
        OrdersExample.Criteria criteria = example.createCriteria();
        criteria.andSpencerIdEqualTo(spencer.getSpencerId());
        List<Orders> orders = mapper.selectByExample(example);
        criteria.andCreateTimeEqualTo(new Date());
        List<Orders> orders2 = mapper.selectByExample(example);
        Map<String,Integer> map = new HashMap<>();
        map.put("data_size",orders.size());
        map.put("data_new",orders2.size());
        return map;
    }

    /**
     * 模糊查询和查询所有数据
     * @param pageNum
     * @param pageSize
     * @param orders
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,readOnly = true)//readOnly=true查询到的数据只读
    public PageInfo<Orders> queryAll(Integer pageNum, Integer pageSize, Orders orders){
        if(Objects.equals(orders.getPhone(), "")){
            orders.setPhone(null);
        }
        if(Objects.equals(orders.getUserName(), "")){
            orders.setUserName(null);
        }
        //分页
        PageHelper.startPage(pageNum,pageSize);//只对紧邻的查询语句有效，是用拦截器的方式实现
        List<Orders> list = mapper.selectBy(orders);
        return new PageInfo<>(list);
    }

    /**
     * 新增预约信息
     * @param orders
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int addOrder(Orders orders){
        return mapper.insertSelective(orders);
    }

    /**
     * 修改预约状态
     * @param orders
     * @return
     */
    @Transactional(propagation = Propagation.REQUIRED,rollbackFor = {Exception.class})
    public int updateOrder(Orders orders){
        return mapper.updateByPrimaryKeySelective(orders);
    }
}
